home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / arp.c next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  9.3 KB  |  342 lines

  1. /* Address Resolution Protocol (ARP) functions. Sits between IP and
  2.  * Level 2, mapping IP to Level 2 addresses for all outgoing datagrams.
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "timer.h"
  7. #include "iface.h"
  8. #include "enet.h"
  9. #include "ax25.h"
  10. #include "icmp.h"
  11. #include "ip.h"
  12. #include "arp.h"
  13. #include "icmp.h"
  14. #include "rspf.h"
  15.  
  16. static unsigned arp_hash __ARGS((int16 hardware,int32 ipaddr));
  17. static void arp_output __ARGS((struct iface *iface,int16 hardware,int32 target));
  18.  
  19. /* ARP entries for particular subnetwork types. The table values
  20.  * are filled in by calls to arp_init() at device attach time
  21.  */
  22. #define    NTYPES    9
  23. struct arp_type Arp_type[NTYPES];
  24.  
  25. /* Hash table headers */
  26. struct arp_tab *Arp_tab[ARPSIZE];
  27.  
  28. struct arp_stat Arp_stat;
  29.  
  30. /* Initialize an entry in the ARP table
  31.  * Called by the device driver at attach time
  32.  */
  33. int
  34. arp_init(hwtype,hwalen,iptype,arptype,pendtime,bdcst,format,scan)
  35. unsigned int hwtype;    /* ARP Hardware type */
  36. int hwalen;        /* Hardware address length */
  37. int iptype;        /* Subnet's protocol ID for IP */
  38. int arptype;        /* Subnet's protocol ID for ARP */
  39. int pendtime;        /* # secs to wait pending response */
  40. char *bdcst;        /* Subnet's broadcast address (if any) */
  41. char *(*format)();    /* Function to format hardware addresses */
  42. int (*scan)();        /* Function to scan addresses in ascii */    
  43. {
  44.     register struct arp_type *at;
  45.  
  46.     if(hwtype >= NTYPES)
  47.         return -1;    /* Table too small */
  48.  
  49.     at = &Arp_type[hwtype];
  50.     at->hwalen = (int16)hwalen;
  51.     at->iptype = (int16)iptype;
  52.     at->arptype = (int16)arptype;
  53.     at->pendtime = (int16)pendtime;
  54.     at->bdcst = bdcst;
  55.     at->format = format;
  56.     at->scan = scan;
  57.     return 0;
  58. }
  59.  
  60. /* Resolve an IP address to a hardware address; if not found,
  61.  * initiate query and return NULLCHAR.  If an address is returned, the
  62.  * interface driver may send the packet; if NULLCHAR is returned,
  63.  * res_arp() will have saved the packet on its pending queue,
  64.  * so no further action (like freeing the packet) is necessary.
  65.  */
  66. char *
  67. res_arp(iface,hardware,target,bp)
  68. struct iface *iface;    /* Pointer to interface block */
  69. int16 hardware;        /* Hardware type */
  70. int32 target;        /* Target IP address */
  71. struct mbuf *bp;    /* IP datagram to be queued if unresolved */
  72. {
  73.     register struct arp_tab *arp;
  74.     struct ip ip;
  75.  
  76.     if((arp = arp_lookup(hardware,target)) != NULLARP && arp->state == ARP_VALID)
  77.         return arp->hw_addr;
  78.     if(arp != NULLARP){
  79.         /* Earlier packets are already pending, kick this one back
  80.          * as a source quench
  81.          */
  82.         ntohip(&ip,&bp);
  83.         icmp_output(&ip,bp,ICMP_QUENCH,0,NULL);
  84.         free_p(bp);
  85.     } else {
  86.         /* Create an entry and put the datagram on the
  87.          * queue pending an answer
  88.          */
  89.         arp = arp_add(target,hardware,NULLCHAR,0);
  90.         enqueue(&arp->pending,bp);
  91.         arp_output(iface,hardware,target);
  92.     }
  93.     return NULLCHAR;
  94. }
  95. /* Handle incoming ARP packets. This is almost a direct implementation of
  96.  * the algorithm on page 5 of RFC 826, except for:
  97.  * 1. Outgoing datagrams to unresolved addresses are kept on a queue
  98.  *    pending a reply to our ARP request.
  99.  * 2. The names of the fields in the ARP packet were made more mnemonic.
  100.  * 3. Requests for IP addresses listed in our table as "published" are
  101.  *    responded to, even if the address is not our own.
  102.  */
  103. void
  104. arp_input(iface,bp)
  105. struct iface *iface;
  106. struct mbuf *bp;
  107. {
  108.     struct arp arp;
  109.     struct arp_tab *ap;
  110.     struct arp_type *at;
  111.     
  112.     Arp_stat.recv++;
  113.     if(ntoharp(&arp,&bp) == -1)    /* Convert into host format */
  114.         return;
  115.     if(arp.hardware >= NTYPES){
  116.         /* Unknown hardware type, ignore */
  117.         Arp_stat.badtype++;
  118.         return;
  119.     }
  120.     at = &Arp_type[arp.hardware];
  121.     if(arp.protocol != at->iptype){
  122.         /* Unsupported protocol type, ignore */
  123.         Arp_stat.badtype++;
  124.         return;
  125.     }
  126.     if(uchar(arp.hwalen) > MAXHWALEN || uchar(arp.pralen) != sizeof(int32)){
  127.         /* Incorrect protocol addr length (different hw addr lengths
  128.          * are OK since AX.25 addresses can be of variable length)
  129.          */
  130.         Arp_stat.badlen++;
  131.         return;
  132.     }
  133.     if(memcmp(arp.shwaddr,at->bdcst,at->hwalen) == 0){
  134.         /* This guy is trying to say he's got the broadcast address! */
  135.         Arp_stat.badaddr++;
  136.         return;
  137.     }
  138.     /* If this guy is already in the table, update its entry
  139.      * unless it's a manual entry (noted by the lack of a timer)
  140.      */
  141.     ap = NULLARP;    /* ap plays the role of merge_flag in the spec */
  142.     if((ap = arp_lookup(arp.hardware,arp.sprotaddr)) != NULLARP
  143.      && ap->timer.start != 0){
  144.         ap = arp_add(arp.sprotaddr,arp.hardware,arp.shwaddr,0);
  145.     }
  146.     /* See if we're the address they're looking for */
  147.     if(ismyaddr(arp.tprotaddr) != NULLIF){
  148.         if(ap == NULLARP)    /* Only if not already in the table */
  149.             arp_add(arp.sprotaddr,arp.hardware,arp.shwaddr,0);
  150.  
  151.         if(arp.opcode == ARP_REQUEST){
  152.             /* Swap sender's and target's (us) hardware and protocol
  153.              * fields, and send the packet back as a reply
  154.              */
  155.             memcpy(arp.thwaddr,arp.shwaddr,(int16)uchar(arp.hwalen));
  156.             /* Mark the end of the sender's AX.25 address
  157.              * in case he didn't
  158.              */
  159.             if(arp.hardware == ARP_AX25)
  160.                 arp.thwaddr[uchar(arp.hwalen)-1] |= E;
  161.  
  162.             memcpy(arp.shwaddr,iface->hwaddr,at->hwalen);
  163.             arp.tprotaddr = arp.sprotaddr;
  164.             arp.sprotaddr = iface->addr;
  165.             arp.opcode = ARP_REPLY;
  166.             if((bp = htonarp(&arp)) == NULLBUF)
  167.                 return;
  168.  
  169.             if(iface->forw != NULLIF)
  170.                 (*iface->forw->output)(iface->forw,
  171.                  arp.thwaddr,iface->forw->hwaddr,at->arptype,bp);
  172.             else 
  173.                 (*iface->output)(iface,arp.thwaddr,
  174.                  iface->hwaddr,at->arptype,bp);
  175.             Arp_stat.inreq++;
  176. #ifdef    RSPF
  177.             /* Do an RSPF upcall */
  178.             rspfarpupcall(arp.tprotaddr,arp.hardware,NULLIF);
  179. #endif    /* RSPF*/
  180.         } else {
  181.             Arp_stat.replies++;
  182. #ifdef    RSPF
  183.             /* Do an RSPF upcall */
  184.             rspfarpupcall(arp.sprotaddr,arp.hardware,iface);
  185. #endif    /* RSPF*/
  186.         }
  187.     } else if(arp.opcode == ARP_REQUEST
  188.      && (ap = arp_lookup(arp.hardware,arp.tprotaddr)) != NULLARP
  189.      && ap->pub){
  190.         /* Otherwise, respond if the guy he's looking for is
  191.          * published in our table.
  192.          */
  193.         memcpy(arp.thwaddr,arp.shwaddr,(int16)uchar(arp.hwalen));
  194.         memcpy(arp.shwaddr,ap->hw_addr,at->hwalen);
  195.         arp.tprotaddr = arp.sprotaddr;
  196.         arp.sprotaddr = ap->ip_addr;
  197.         arp.opcode = ARP_REPLY;
  198.         if((bp = htonarp(&arp)) == NULLBUF)
  199.             return;
  200.         if(iface->forw != NULLIF)
  201.             (*iface->forw->output)(iface->forw,
  202.              arp.thwaddr,iface->forw->hwaddr,at->arptype,bp);
  203.         else 
  204.             (*iface->output)(iface,arp.thwaddr,
  205.              iface->hwaddr,at->arptype,bp);
  206.         Arp_stat.inreq++;
  207.     }
  208. }
  209. /* Add an IP-addr / hardware-addr pair to the ARP table */
  210. struct arp_tab *
  211. arp_add(ipaddr,hardware,hw_addr,pub)
  212. int32 ipaddr;        /* IP address, host order */
  213. int16 hardware;        /* Hardware type */
  214. char *hw_addr;        /* Hardware address, if known; NULLCHAR otherwise */
  215. int pub;        /* Publish this entry? */
  216. {
  217.     struct mbuf *bp;
  218.     register struct arp_tab *ap;
  219.     struct arp_type *at;
  220.     unsigned hashval;
  221.  
  222.     if(hardware >=NTYPES)
  223.         return NULLARP;    /* Invalid hardware type */
  224.     at = &Arp_type[hardware];
  225.  
  226.     if((ap = arp_lookup(hardware,ipaddr)) == NULLARP){
  227.         /* New entry */
  228.         ap = (struct arp_tab *)callocw(1,sizeof(struct arp_tab));
  229.         ap->hw_addr = mallocw(at->hwalen);
  230.         ap->timer.func = arp_drop;
  231.         ap->timer.arg = ap;
  232.         ap->hardware = hardware;
  233.         ap->ip_addr = ipaddr;
  234.  
  235.         /* Put on head of hash chain */
  236.         hashval = arp_hash(hardware,ipaddr);
  237.         ap->prev = NULLARP;
  238.         ap->next = Arp_tab[hashval];
  239.         Arp_tab[hashval] = ap;
  240.         if(ap->next != NULLARP){
  241.             ap->next->prev = ap;
  242.         }
  243.     }
  244.     if(hw_addr == NULLCHAR){
  245.         /* Await response */
  246.         ap->state = ARP_PENDING;
  247.         ap->timer.start = Arp_type[hardware].pendtime * (1000 / MSPTICK);
  248.     } else {
  249.         /* Response has come in, update entry and run through queue */
  250.         ap->state = ARP_VALID;
  251.         ap->timer.start = ARPLIFE * (1000 / MSPTICK);
  252.         memcpy(ap->hw_addr,hw_addr,at->hwalen);
  253.         ap->pub = pub;
  254.         while((bp = dequeue(&ap->pending)) != NULLBUF)
  255.             ip_route(NULLIF,bp,0);
  256.     }
  257.     start_timer(&ap->timer);
  258.     return ap;
  259. }
  260.  
  261. /* Remove an entry from the ARP table */
  262. void
  263. arp_drop(p)
  264. void *p;
  265. {
  266.     register struct arp_tab *ap;
  267.  
  268.     ap = (struct arp_tab *)p;
  269.     if(ap == NULLARP)
  270.         return;
  271.     stop_timer(&ap->timer);    /* Shouldn't be necessary */
  272.     if(ap->next != NULLARP)
  273.         ap->next->prev = ap->prev;
  274.     if(ap->prev != NULLARP)
  275.         ap->prev->next = ap->next;
  276.     else
  277.         Arp_tab[arp_hash(ap->hardware,ap->ip_addr)] = ap->next;
  278.     free_q(&ap->pending);
  279.     free(ap->hw_addr);
  280.     free((char *)ap);
  281. }
  282.  
  283. /* Look up the given IP address in the ARP table */
  284. struct arp_tab *
  285. arp_lookup(hardware,ipaddr)
  286. int16 hardware;
  287. int32 ipaddr;
  288. {
  289.     register struct arp_tab *ap;
  290.  
  291.     for(ap = Arp_tab[arp_hash(hardware,ipaddr)]; ap != NULLARP; ap = ap->next){
  292.         if(ap->ip_addr == ipaddr && ap->hardware == hardware)
  293.             break;
  294.     }
  295.     return ap;
  296. }
  297. /* Send an ARP request to resolve IP address target_ip */
  298. static void
  299. arp_output(iface,hardware,target)
  300. struct iface *iface;
  301. int16 hardware;
  302. int32 target;
  303. {
  304.     struct arp arp;
  305.     struct mbuf *bp;
  306.     struct arp_type *at;
  307.  
  308.     at = &Arp_type[hardware];
  309.     if(iface->output == NULLFP)
  310.         return;
  311.     
  312.     arp.hardware = hardware;
  313.     arp.protocol = at->iptype;
  314.     arp.hwalen = at->hwalen;
  315.     arp.pralen = sizeof(int32);
  316.     arp.opcode = ARP_REQUEST;
  317.     memcpy(arp.shwaddr,iface->hwaddr,at->hwalen);
  318.     arp.sprotaddr = iface->addr;
  319.     memset(arp.thwaddr,0,at->hwalen);
  320.     arp.tprotaddr = target;
  321.     if((bp = htonarp(&arp)) == NULLBUF)
  322.         return;
  323.     (*iface->output)(iface,at->bdcst,
  324.         iface->hwaddr,at->arptype,bp);
  325.     Arp_stat.outreq++;
  326. }
  327.  
  328. /* Hash a {hardware type, IP address} pair */
  329. static
  330. unsigned
  331. arp_hash(hardware,ipaddr)
  332. int16 hardware;
  333. int32 ipaddr;
  334. {
  335.     register unsigned hashval;
  336.  
  337.     hashval = hardware;
  338.     hashval ^= hiword(ipaddr);
  339.     hashval ^= loword(ipaddr);
  340.     return hashval % ARPSIZE;
  341. }        
  342.